package com.youxin.foundation.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.NumberFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @ClassName: UID
 * @Description: 订单号生成
 * @author yingde.cao
 * @date 2017年11月28日 上午11:01:24
 *
 */
public class GenerateIdUtils {
	
	private static AtomicLong atomicLong = new AtomicLong(0L);
	private static AtomicLong timestamp = new AtomicLong(System.currentTimeMillis());
	private static final Long DEFAULT_MAX = 999L;
	private static String ip_postfix;
	private static NumberFormat numberFormat;
	private static final String ORDER_PREFIX= "YX";

	static {
		try {
			InetAddress ia = InetAddress.getLocalHost();
			String[] split = ia.getHostAddress().split("\\.");
			ip_postfix = split[split.length - 1];
			NumberFormat numberFormat = NumberFormat.getNumberInstance();
			numberFormat.setGroupingUsed(false);
			numberFormat.setMinimumIntegerDigits(3);
			ip_postfix = numberFormat.format(Long.parseLong(ip_postfix));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		numberFormat = NumberFormat.getNumberInstance();
		int seuquenceLen = String.valueOf(DEFAULT_MAX).length();
		numberFormat.setMinimumIntegerDigits(seuquenceLen);
		numberFormat.setGroupingUsed(false);
	}

	public static void main(String[] args) {
		System.out.println(getUniqueId());
	}
	
	public static String getUniqueId() {
		String formatSequence = numberFormat.format(getSequence(DEFAULT_MAX));
		return ip_postfix + System.currentTimeMillis() + formatSequence;
	}

	@SuppressWarnings("unused")
	private static String getUniqueId(Long maxPerMillisecond) {
		if (maxPerMillisecond == null)
			maxPerMillisecond = DEFAULT_MAX;
		NumberFormat numberFormat = NumberFormat.getNumberInstance();
		numberFormat.setMinimumIntegerDigits(String.valueOf(DEFAULT_MAX).length());
		Long sequence = getSequence(maxPerMillisecond);
		String formatSequence = numberFormat.format(sequence);
		return ip_postfix + System.currentTimeMillis() + formatSequence;
	}

	/**
	 * get a sequence（number） in a millisecond,if sequenceNumber equels
	 * maxPerMillisecond, it will return at next millisecond with a sequenceNumber
	 * which will increase from zero again
	 *
	 * @param maxPerMillisecond
	 * @return
	 */
	public static Long getSequence(Long maxPerMillisecond) {
		long nextSequence = atomicLong.get();
		while (nextSequence >= maxPerMillisecond || !atomicLong.compareAndSet(nextSequence, ++nextSequence)) {
			if (nextSequence >= maxPerMillisecond && timestamp.longValue() >= System.currentTimeMillis()) {
				nextSequence = atomicLong.get();
			} else if (nextSequence >= maxPerMillisecond) {
				if (atomicLong.compareAndSet(maxPerMillisecond, 0)) {
					timestamp.set(System.currentTimeMillis());
					nextSequence = 0L;
				}
			}
		}
		return nextSequence;
	}
	/**
	 * @Title: getOrderSn 
	 * @Description: 生成订单ID
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getOrderSn() {
		int ran = (int)((Math.random()*9+1)*10000);
		return ORDER_PREFIX+DateUtil.dateToString(new Date(), "yyMMddHH") + ran;
	}
}
